package com.copm.demonstrationsystem.service.introductionfeatures.impl;

import com.copm.demonstrationsystem.commons.response.ResponseStatus;
import com.copm.demonstrationsystem.commons.util.ImageUtils;
import com.copm.demonstrationsystem.config.SystemConfig;
import com.copm.demonstrationsystem.dao.commons.ImageDao;
import com.copm.demonstrationsystem.dao.introductionfeatures.CompanyCultureDao;
import com.copm.demonstrationsystem.dao.introductionfeatures.IntroductionTextDao;
import com.copm.demonstrationsystem.dto.introductionfeatures.CompanyCultureDto;
import com.copm.demonstrationsystem.pojo.commons.Image;
import com.copm.demonstrationsystem.pojo.introductionfeatures.CompanyCulture;
import com.copm.demonstrationsystem.pojo.introductionfeatures.IntroductionText;
import com.copm.demonstrationsystem.service.introductionfeatures.CompanyCultureService;
import com.copm.demonstrationsystem.service.introductionfeatures.commons.MultiImageAndTextHandle;
import com.copm.demonstrationsystem.service.introductionfeatures.commons.SinglonImageHandle;
import com.copm.demonstrationsystem.service.introductionfeatures.commons.TransformImagePath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.util.*;

/**
 * @Author: Yuan Baiyu
 * @Date: Created in 15:48 2018/4/24
 */
@Service
public class CompanyCultureServiceImpl implements CompanyCultureService {

    private static Logger logger = LoggerFactory.getLogger(CompanyCultureServiceImpl.class);

    private Object lock = new Object();

    @Autowired
    private SystemConfig config;

    @Autowired
    IntroductionTextDao introductionTextDao;

    @Autowired
    CompanyCultureDao companyCultureDao;

    @Autowired
    MultiImageAndTextHandle multiImageAndTextHandle;

    @Autowired
    SinglonImageHandle singlonImageHandle;

    @Autowired
    TransformImagePath transformImagePath;

    @Autowired
    ImageDao imageDao;

    //获取“企业文化”页面信息
    @Override
    public ResponseStatus getPage() {
        ResponseStatus status = new ResponseStatus();
        CompanyCultureDto companyCultureDto = new CompanyCultureDto();

        CompanyCulture companyCulture = companyCultureDao.findPage();
        if (null == companyCulture) {
            status.setData("查无“企业文化”页面");
            return status;
        }
        //获取页面的背景图片
        Image backgroundImage = companyCulture.getBackgroundImage();
        //后驱页面的演示图片
        Image demonstrationImage = companyCulture.getDemonstrationImage();
        if (null == backgroundImage) {
            companyCulture.setBackgroundImage(null);
        } else {
            backgroundImage = transformImagePath.transformImagePath(backgroundImage);
        }
        companyCultureDto.setBackgroundImage(backgroundImage);

        if (null == demonstrationImage) {
            companyCulture.setDemonstrationImage(null);
        } else {
            demonstrationImage = transformImagePath.transformImagePath(demonstrationImage);
        }
        companyCultureDto.setDemonstrationImage(demonstrationImage);
        //获取页面的所有标签文本
        List<IntroductionText> introductionTexts = introductionTextDao.findByTextKind(2L);
        //以TreeMap为底层结构，sequence为键排序
        Map<Long, IntroductionText> treeMapText = new TreeMap<>();
        for (IntroductionText lists : introductionTexts) {
            if (null == lists.getSequence()) {
                continue;
            }
            treeMapText.put(lists.getSequence(), lists);
        }
        //排列好的文本内容
        List<IntroductionText> titles = new ArrayList<>();
        Set<Long> keySet = treeMapText.keySet();
        //遍历key集合，获取value
        for (Long key : keySet) {
            titles.add(treeMapText.get(key));
        }
        companyCultureDto.setTitle(titles);


        //获取页面的所有内容文本
        List<IntroductionText> introductionContents = introductionTextDao.findByTextKind(8L);
        //以TreeMap为底层结构，sequence为键排序
        Map<Long, IntroductionText> treeMapContents = new TreeMap<>();
        for (IntroductionText lists : introductionContents) {
            if (null == lists.getSequence()) {
                continue;
            }
            treeMapContents.put(lists.getSequence(), lists);
        }
        //排列好的文本内容
        List<IntroductionText> contents = new ArrayList<>();
        //遍历key集合，获取value
        for (Long key : keySet) {
            contents.add(treeMapContents.get(key));
        }
        companyCultureDto.setContent(contents);
        //保存入返回类型
        status.setStatue(1);
        status.setData(companyCultureDto);
        logger.info("“企业文化”页面信息获取成功");
        return status;
    }

    //添加“企业文化”页面演示图片(图片最大数量只能为1，但未拦截过量请求)
    @Override
    public ResponseStatus addCompanyCultureDemonstrationImage(MultipartFile file) {
        ResponseStatus status = singlonImageHandle.storeAndCheckImage(file);
        if (status.getStatue() == 0) {
            return status;
        }
        //获取图片存储信息
        String[] paths = (String[]) status.getData();
        Image image = new Image(paths[0], paths[1], paths[2]);
        //获取“企业文化”页面对象
        CompanyCulture companyCulture = companyCultureDao.findPage();
        if (null == companyCulture) {
            companyCulture = new CompanyCulture();
        }
        //设置“企业文化”页面图片
        companyCulture.setDemonstrationImage(image);
        //保存添加了图片的对象
        companyCultureDao.save(companyCulture);
        status.setStatue(1);
        logger.info("“企业文化”页面演示图片添加成功");
        return status;
    }

    @Override
    public ResponseStatus addCompanyCultureBackgroundImage(MultipartFile file) {
        ResponseStatus status = singlonImageHandle.storeAndCheckImage(file);
        if (status.getStatue() == 0) {
            return status;
        }
        //获取图片存储信息
        String[] paths = (String[]) status.getData();
        Image image = new Image(paths[0], paths[1], paths[2]);
        //获取“企业文化”页面对象
        CompanyCulture companyCulture = companyCultureDao.findPage();
        if (null == companyCulture) {
            companyCulture = new CompanyCulture();
        }
        //设置“企业文化”页面图片
        companyCulture.setBackgroundImage(image);
        //保存添加了图片的对象
        companyCultureDao.save(companyCulture);
        status.setStatue(1);
        logger.info("“企业文化”页面背景图片添加成功");
        return status;
    }


    //删除“企业文化”页面演示图片
    @Override
    public ResponseStatus deleteCompanyCultureDemonstrationImage() {
        ResponseStatus status;
        //获取页面对象
        CompanyCulture companyCulture = companyCultureDao.findPage();
        //获取图片名
        Image image = companyCulture.getDemonstrationImage();
        status = singlonImageHandle.deleteImage(image);
        if (status.getStatue() == 0) {
            return status;
        }
        //临时保存图片ID
        Long tempId = image.getId();
        //获取“企业文化”页面 ，设置图片为空
        companyCulture.setDemonstrationImage(null);
        //保存已删除的页面对象
        companyCultureDao.save(companyCulture);
        //从图片对象中删除页面所对应的图片数据
        imageDao.delete(tempId);

        status.setStatue(1);
        logger.info("“企业文化”页面演示图片删除成功");
        return status;
    }

    /**
     * 删除“企业文化”页面背景图片
     *
     * @return
     */
    @Override
    public ResponseStatus deleteCompanyCultureBackgroundImage() {
        ResponseStatus status;
        //获取页面对象
        CompanyCulture companyCulture = companyCultureDao.findPage();
        //获取图片名
        Image image = companyCulture.getBackgroundImage();
        status = singlonImageHandle.deleteImage(image);
        if (status.getStatue() == 0) {
            return status;
        }
        //临时保存图片ID
        Long tempId = image.getId();
        //获取“企业文化”页面 ，设置图片为空
        companyCulture.setBackgroundImage(null);
        //保存已删除的页面对象
        companyCultureDao.save(companyCulture);
        //从图片对象中删除页面所对应的图片数据
        imageDao.delete(tempId);

        status.setStatue(1);
        logger.info("“企业文化”页面背景图片删除成功");
        return status;
    }


    //修改“企业文化”页面演示图片
    @Override
    public ResponseStatus modifyCompanyCultureDemonstrationImage(MultipartFile file) {
        ResponseStatus status;
        //查找页面
        CompanyCulture companyCulture = companyCultureDao.findPage();
        //对象判空，即代表此页面对象不存在，需要先创建
        synchronized (lock) {
            if (null == companyCulture) {
                companyCulture = new CompanyCulture();
                //保存对象
                companyCultureDao.save(companyCulture);
            }
        }
        if (null != companyCulture.getDemonstrationImage()) {
            status = deleteCompanyCultureDemonstrationImage();
            if (status.getStatue() == 0) {
                return status;
            }
        }
        //添加图片
        status = addCompanyCultureDemonstrationImage(file);
        logger.info("“企业文化”页面演示图片修改成功");
        return status;
    }

    //修改 背景图片
    @Override
    public ResponseStatus modifyCompanyCultureBackgroundImage(MultipartFile file) {
        ResponseStatus status;
        //查找页面
        CompanyCulture companyCulture = companyCultureDao.findPage();
        //对象判空，即代表此页面对象不存在，需要先创建
        synchronized (lock) {
            if (null == companyCulture) {
                companyCulture = new CompanyCulture();
                //保存对象
                companyCultureDao.save(companyCulture);
            }
        }
        if (null != companyCulture.getBackgroundImage()) {
            status = deleteCompanyCultureBackgroundImage();
            if (status.getStatue() == 0) {
                return status;
            }
        }
        //添加图片
        status = addCompanyCultureBackgroundImage(file);
        logger.info("“企业文化”页面背景图片修改成功");
        return status;
    }

    //添加“企业文化”页面标签文本
    @Override
    public ResponseStatus addCompanyCultureTitle(String title, Long seq) {
        ResponseStatus status = multiImageAndTextHandle.addIntroductionText(title, 2L, seq);
        logger.info("“企业文化”页面标签文本添加成功");
        return status;
    }

    //添加“企业文化”页面内容文本
    @Override
    public ResponseStatus addCompanyCultureContent(String content, Long seq) {
        ResponseStatus status = multiImageAndTextHandle.addIntroductionText(content, 8L, seq);
        logger.info("“企业文化”页面内容文本添加成功");
        return status;
    }


    //删除“企业文化”页面标签文本
    @Override
    public ResponseStatus deleteCompanyCultureTitle(Long id) {
        ResponseStatus status = multiImageAndTextHandle.deleteIntroductionText(id, 2L);
        if (0 == status.getStatue()) {
            return status;
        }
        logger.info("“企业文化”页面标签文本删除成功");
        return status;
    }

    //删除“企业文化”页面内容文本
    @Override
    public ResponseStatus deleteCompanyCultureContent(Long id) {
        ResponseStatus status = multiImageAndTextHandle.deleteIntroductionText(id, 8L);
        if (0 == status.getStatue()) {
            return status;
        }
        logger.info("“企业文化”页面内容文本删除成功");
        return status;
    }

    //修改“企业文化”页面标签文本
    @Override
    public ResponseStatus modifyCompanyCultureTitle(Long id, String title) {
        ResponseStatus status = new ResponseStatus();
        if (null == id || null == introductionTextDao.findOne(id)) {
            status.setStatue(0);
            status.setMessage("无ID对应文本");
            return status;
        }
        Long tempSEQ = introductionTextDao.findOne(id).getSequence();
        //删除待修改的原标签文本
        status = deleteCompanyCultureTitle(id);
        if (0 == status.getStatue()) {
            return status;
        }
        //添加待修改的标签文本
        if (null == tempSEQ) {
            status.setStatue(0);
            status.setMessage("此文本无序列号，无法修改");
            return status;
        }
        status = addCompanyCultureTitle(title, tempSEQ);
        if (0 == status.getStatue()) {
            return status;
        }
        logger.info("“企业文化”页面标签文本修改成功");
        return status;
    }


    //修改“企业文化”页面内容文本
    @Override
    public ResponseStatus modifyCompanyCultureContent(Long id, String content) {
        Long tempSEQ = introductionTextDao.findOne(id).getSequence();
        //删除待修改的原内容文本
        ResponseStatus status = deleteCompanyCultureContent(id);
        if (0 == status.getStatue()) {
            return status;
        }
        //添加待修改的标签文本
        status = addCompanyCultureContent(content, tempSEQ);
        if (0 == status.getStatue()) {
            return status;
        }
        logger.info("“企业文化”页面内容文本修改成功");
        return status;
    }
}
